package com.only4play.codegen.processor.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.google.auto.service.AutoService;
import com.only4play.codegen.context.DefaultNameContext;
import com.only4play.codegen.processor.BaseCodeGenProcessor;
import com.only4play.codegen.processor.CodeGenProcessor;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeSpec;

import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import java.lang.annotation.Annotation;
import java.util.Optional;

/**
 * @author dmy
 */
@AutoService(value = CodeGenProcessor.class)
public class GenMapperProcessor extends BaseCodeGenProcessor {

  public static final String REPOSITORY_SUFFIX = "Mapper";

  @Override
  protected void generateClass(TypeElement typeElement, RoundEnvironment roundEnvironment) {
    String className = typeElement.getSimpleName() + REPOSITORY_SUFFIX;
    TypeSpec.Builder typeSpecBuilder = TypeSpec.interfaceBuilder(className)
        .addSuperinterface(ParameterizedTypeName.get(ClassName.get(BaseMapper.class), ClassName.get(typeElement)))
            .addJavadoc("@author mdy")
        .addModifiers(Modifier.PUBLIC);

    genJavaSourceFile(generatePackage(typeElement),typeElement.getAnnotation(GenMapper.class).sourcePath(),typeSpecBuilder);
  }

  @Override
  public Class<? extends Annotation> getAnnotation() {
    return GenMapper.class;
  }

  @Override
  public String generatePackage(TypeElement typeElement) {
    return typeElement.getAnnotation(GenMapper.class).pkgName();
  }

  public GenMapperProcessor() {
  }
}
